#!/bin/sh
### BEGIN INIT INFO
# Provides:          go-carbon
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: go-carbon carbon daemon
### END INIT INFO

DAEMON="/usr/bin/go-carbon"
NAME=$(basename $DAEMON)
PIDFILE="/var/run/go-carbon.pid"
CONFIG="/etc/go-carbon/go-carbon.conf"

# Avoid using root's TMPDIR
unset TMPDIR

# redirect to systemd in centos
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
fi

if [ -f /etc/sysconfig/$NAME ]; then
   . /etc/sysconfig/$NAME
fi

ARGS="-config $CONFIG -pidfile $PIDFILE -daemon"

# Check that config exists.
[ -f $config ] || exit 6

PID=""
if [ -f $PIDFILE ]; then
  PID=$(cat $PIDFILE)
fi

RETVAL=0

BOOTUP=serial
MOVE_TO_COL=
SETCOLOR_SUCCESS=
SETCOLOR_FAILURE=
SETCOLOR_WARNING=
SETCOLOR_NORMAL=

if test -t 1; then
  BOOTUP=color
  RES_COL=60
  MOVE_TO_COL="\\033[${RES_COL}G"
  SETCOLOR_SUCCESS="\\033[1;32m"
  SETCOLOR_FAILURE="\\033[1;31m"
  SETCOLOR_WARNING="\\033[1;33m"
  SETCOLOR_NORMAL="\\033[0;39m"
  LOGLEVEL=1
fi

echo_success() {
  [ "$BOOTUP" = "color" ] && echo -n $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_SUCCESS
  echo -n "  OK  "
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_NORMAL
  echo -n "]"
  echo -n "\r"
  return 0
}

echo_failure() {
  [ "$BOOTUP" = "color" ] && echo -n $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_FAILURE
  echo -n "FAILED"
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_NORMAL
  echo -n "]"
  echo -n "\r"
  return 1
}

echo_warning() {
  [ "$BOOTUP" = "color" ] && echo -n $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_WARNING
  echo -n "WARNING"
  [ "$BOOTUP" = "color" ] && echo -n $SETCOLOR_NORMAL
  echo -n "]"
  echo -n "\r"
  return 1
}

# Log that something succeeded
success() {
  [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
  return 0
}

# Log that something failed
failure() {
  local rc=$?
  [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
  [ -x /bin/plymouth ] && /bin/plymouth --details
  return $rc
}

# Log a warning
warning() {
  local rc=$?
  [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
  return $rc
}

is_running() {
  if [ -z "${PID}" ]; then
    return 1
  fi

  if ! [ -f "/proc/${PID}/stat" ]; then
    return 1
  fi

  current_prog=$(cat /proc/${PID}/stat 2>/dev/null | awk '{print $2}')
  if ! [ "${current_prog}" = "(${NAME})" ]; then
    return 1
  fi

  return 0
}

configtest() {
  $DAEMON $ARGS -check-config
}

start() {
  echo -n "Starting daemon $NAME : "

  if is_running; then
    RETVAL=1
    echo -n "Already running !" && warning
    echo
  else
    $DAEMON $ARGS
    RETVAL=$?
    [ $RETVAL -eq 0 ] && success || failure
    echo
  fi
}

stop() {
  if ! is_running; then
    return
  fi

  echo -n "Stopping daemon $NAME : "

  kill -USR2 $PID
 
  while true; do
    if ! is_running; then
      break
    fi
    # sleep 0.1
  done

  if [ -f "$PIDFILE" ]; then
    rm -f "$PIDFILE"
  fi

  success
  echo
}

restart() {
  configtest || return $?
  stop
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    if is_running; then
      kill -HUP $PID
    fi
    ;;
  configtest)
    configtest
    ;;
  status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|configtest|status}"
    exit 1
    ;;
esac

exit $RETVAL
